<template>
  <div>
    <el-form :model="form">
      <el-form-item label="名称" prop="sensorName">
        <el-input v-model="form.sensorName" disabled/>
      </el-form-item>

      <el-form-item label="采集开关" prop="triggerStatus">
        <el-switch
            @change="changeTriggerStatus"
            v-model="form.triggerStatus"
            active-color="#13ce66"
            active-text="开启"
            inactive-text="停止"
        >
        </el-switch>
      </el-form-item>

      <el-form-item label="采集频率(秒)" prop="speed">
        <el-input v-model="form.speed" type="number">
          <el-button slot="append" @click="changeSpeed">设置</el-button>
        </el-input>
      </el-form-item>

    </el-form>

  </div>
</template>

<script>

import { getSensor, changeTriggerStatus, changeSpeed } from '@/api/equipment/sensor'

export default {
  name: 'index',
  props: {
    sensor: {
      type: Object
    }
  },
  data() {
    return {
      timer: null,
      loadingTime: 20,
      loading: false,
      btnText: '读取',
      form: {}
    }
  },
  created() {
    this.form = this.sensor
  },
  methods: {

    changeTriggerStatus(val) {
      // console.log('新状态的值', val)
      this.form.triggerStatus = val
      let text = null
      if (val) {
        text = '已开启'
      } else {
        text = '已停止'
      }
      changeTriggerStatus(this.form).then(response => {
        this.$message.success(text)
      })
    },

    changeSpeed() {
      changeSpeed(this.form).then(response => {
        this.$message.success('设置成功')
      })
    },

    readSensor() {
      this.loading = true
    },

    querySensor() {
      getSensor(this.form.sensorId).then(response => {
        console.log('重新获取结果', response.data)
        this.form.sensorValue = response.data.sensorValue
        this.form.sensorStatus = response.data.sensorStatus
      })
    }
  }
}
</script>

<style scoped>

</style>
